# 基于pytest编写测试脚本
#    + 准备测试数据
#    + 获取响应数据 = 调用接口类().接口方法(传递测试数据)
#    + 断言
#    + 关联

# 导包
# import pytest
from api.login import LoginAPI


# 创建测试类
class TestLoginAPI:
    # 前置处理
    def setup(self):
        # 实例化接口类
        self.login_api = LoginAPI()

    # 后置处理
    def teardown(self):
        pass

    # 登录成功
    def test01_success(self):
        login_data = {
            "mobile": "13800000002",
            "password": "123456"
        }
        response = self.login_api.login(login_data)
        print(response.json())
        # 断言
        assert response.status_code == 200
        assert response.json().get("success") is True
        assert response.json().get("code") == 10000
        assert "成功" in response.text

    # 登录失败（账号错误）
    def test02_mobile_error(self):
        login_data = {
            "mobile": "",
            "password": "123456"
        }
        response = self.login_api.login(login_data)
        print(response.json())
        # 断言
        assert response.status_code == 200
        assert response.json().get("success") is False
        assert response.json().get("code") == 20001
        assert "错误" in response.text

    # 登录失败（密码错误）
    def test03_password_error(self):
        login_data = {
            "mobile": "13800000002",
            "password": "123457"
        }
        response = self.login_api.login(login_data)
        print(response.json())
        # 断言
        assert response.status_code == 200
        assert response.json().get("success") is False
        assert response.json().get("code") == 20001
        assert "错误" in response.text
